package main

import (
	"sync"
	"testing"
)

type singleton struct {
	Value int
}

type Singleton interface {
	getValue() int
}

func (s singleton) getValue() int {
	return s.Value
}

var (
	instance *singleton
	once     sync.Once
)

func GetInstance(v int) Singleton {
	once.Do(func() {
		instance = &singleton{Value: v}
	})
	return instance
}

func TestSingleton(t *testing.T) {
	ins1 := GetInstance(1)
	ins2 := GetInstance(2)

	if ins1 != ins2 {
		t.Fatal("不同实例")
	}
}
